//author: create by SunJH
//时间: 2021/12/2 0002
//邮箱: 965989158@qq.com
//描述: 侧滑删除库测试

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_app/counter/slidable_controller.dart';
import 'package:flutter_slidable/flutter_slidable.dart' hide SlidableController;
import 'package:get/get.dart';

class SlidableTest extends GetView<SlidableController> {
  const SlidableTest({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put<SlidableController>(SlidableController());
    var url = 0;
    return Scaffold(
      body: Center(
        child: Container(
          child: Row(
            children: [
              Obx(() => Text(controller.versionInfo.value.downloadUrl??"")),
              InkWell(child: Text("增加"),onTap: (){
                controller.versionInfo.update((val) {
                  url++;
                  val?.downloadUrl = url.toString();
                });
              },),
            ],
          ),
        ),
      ),
    );
  }

  createOrdinaryList() {
    return ListView(
      children: controller.datas.map((element) {
        return Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(20),
          child: Text(element.toString()),
        );
      }).toList(),
    );
  }

  createSlidableList() {
    return Obx(() {
      return ListView(
        children: controller.datas.map((element) {
          return createSlidable(element);
        }).toList(),
      );
    });
  }

  Widget createSlidable(element) {
    return Slidable(
      // Specify a key if the Slidable is dismissible.
      key: const ValueKey(0),
      // The start action pane is the one at the left or the top side.
      startActionPane: ActionPane(
        // A motion is a widget used to control how the pane animates.
        motion: const ScrollMotion(),

        // A pane can dismiss the Slidable.
        dismissible: DismissiblePane(onDismissed: () {
          print("标记是${key?.reactive ?? -1}");
        }),

        // All actions are defined in the children parameter.
        children: const [
          // A SlidableAction can have an icon and/or a label.
          SlidableAction(
            onPressed: doNothing,
            backgroundColor: Color(0xFFFE4A49),
            foregroundColor: Colors.white,
            icon: Icons.delete,
            label: 'Delete',
          ),
          SlidableAction(
            onPressed: doNothing,
            backgroundColor: Color(0xFF21B7CA),
            foregroundColor: Colors.white,
            icon: Icons.share,
            label: 'Share',
          ),
        ],
      ),
      // The child of the Slidable is what the user sees when the
      // component is not dragged.
      child: const ListTile(title: Text('Slide me')),
    );
  }

  createDismissibleList() {
    return ListView(
      children: controller.datas.map((element) {
        return Dismissible(
          key: ValueKey(element.toString()),
          ///拉出到什么程度删除控件
          dismissThresholds: const {DismissDirection.startToEnd: 0.8},
          ///拉出过程中偏移多少
          crossAxisEndOffset: 0.1,
          child: Container(
            alignment: Alignment.center,
            child: Text(
              element.toString(),
              style: const TextStyle(fontSize: 20, color: Colors.white),
            ),
            height: 80,
            color: Colors.red,
          ),
          ///支持拉出的方向
          direction: DismissDirection.startToEnd,
          ///拉出程度监听，当达到dismissThresholds 设置的值时会调用，返回值表示是否删除该item
          confirmDismiss: (direction) async {
            print("confirmDismiss调用");
            print("删除${element.toString()}");
            return true;
          },
        );
      }).toList(),
    );
  }
}

void doNothing(BuildContext context) {
  print("删除数据");
}
